#!/bin/csh -f
# jfd@Tue May 12 15:13:11 WDT 1998
#
# Shell script to convert restraint files using qconvert
# The results will be shown as links in the overview file.
#
# The variable 'aquaroot' must exist and point to the Aqua top directory.
# The 'aqsetup' script should reside in that directory.
#

if ( $#argv != 2 ) goto usage
goto begin

#####################
# usage block
#####################
usage:
    echo 'usage: qconvert.csh dir_numb rst_type '
    echo ''
    echo '   dir_numb:       number of directory at the server side'
    echo '   rst_type:       automatic  disgeo xplor biosym OR diana_disman'
    exit (1)


#####################
# initialization
#####################
begin:
# SAME AS IN PERL SCRIPTS, MANY ARE REDUNDANT TO THIS PROGRAM

# SITE DEPENDANT
    set serv_loc        = "AQUA_LOC/Aqua/server/"
    set serv_dir        = "AQUA_DIR/Aqua/server/"

    set cgi_bin_loc     = "CGI_LOC/Aqua/"
    set cgi_bin_dir     = "CGI_DIR/Aqua/"

    set tmp_loc         = "TMP_LOC/Aqua/"
    set tmp_dir         = "TMP_DIR/Aqua/"

    set webmaster       = "WEBMASTER"

# SITE INDEPENDENT
    set author          = "jurgen\@bmrb.wisc.edu"

    set overview        = "overview.html"
    set logfile         = "server.log"
    set debug           = "false"


    setenv aquaroot $serv_dir/..
    if ( $debug == "true" ) then
        echo 'Aqua program directory:' $aquaroot
    endif
    source $aquaroot/aqsetup

    umask 0000              # File creation mask
    limit cputime     15    # Maximum number of seconds the CPU can spend
                            # on any single process spawned.
                            # This should be half the $cal_time_secs
                            # defined in the calling cgi script.
    limit filesize   100m   # Maximum size of any one file
    limit datasize   100m   # Maximum size of data (including stack)
    limit coredumpsize 0    # Maximum size of core dump file

    
    echo 'QCONVERT - Converting NMR restraints'


    set path = ($path /usr/ruuci11/people/ruu/bin /usr/share/bin /usr/sbin \
                  $cgi_bin_dir )


#####################
# process the options
#####################
options:
switch ( "$2" )
    case "automatic":
	set rsttype = ""
	breaksw
    case "disgeo":
	set rsttype = "-t 1"
	breaksw
    case "xplor":
	set rsttype = "-t 2"
	breaksw
    case "biosym":
	set rsttype = "-t 3"
	breaksw
    case "diana_disman":
	set rsttype = "-t 4"
	breaksw
    default:
        goto usage
endsw


# go to the right dir
    set dir = $tmp_dir/$1

    if ( !(-e $dir ) )then
	echo "Directory $dir doesn't exist"
	exit(1)
    endif
    
    cd $dir
    
# set restraint file
    set proj  = "restraints"
    
# convert uploaded dos file to unix. Macs might go wrong here?
    gawk '{gsub (/\015/,"",$0);print}' $proj.rst > $proj.rst.tmp
    \mv $proj.rst.tmp $proj.rst
    
# run QCONVERT
    echo ''
    echo ''
    echo ''
    echo 'QCONVERT - Running QCONVERT itself'
    
    qconvert $rsttype $proj.rst

    set result = "false"
    foreach type ( noe hb ss tor dis )
	if ( -e $proj.$type ) then
	    set result = "true"
	endif
    end
    
ending:
#   Update the result file

    if ( ! $?result ) then
        set result = "false"
    endif

    if ( $result != "true" ) then
	echo 'QCONVERT - ERROR no result file created'
    endif

    echo 'QCONVERT - Update the result file in dir' $1
    echo 'QCONVERT - result is' $result
    
    echo 'Finished conversion'
    touch finished # This file will be checked for by the perl script
    
    exit(0)
